/**
 * ...
 * @author Franco Ponticelli
 */

package uform.ui.comp;

import uform.property.IProperty;
import uform.property.Property;
import uform.ui.comp.Container;
import uform.ui.comp.Label;
import uform.ui.comp.Control;
import uform.ui.comp.Component;
using Types;

class Field extends Container
{
	public var label(default, null) : Label;
	public var description(default, null) : TextOutput;
	public var control(default, null) : Component;
	
	public function new(?labelText : String, control : Component)
	{
		super();
		
		if (null == control)
			throw "invalid null argument control";
		this.control = control;
		var c : { private var _parent : IProperty<Container>; } = control;
		c._parent.set(this);
		
		label = new Label("_label");
		var c : { private var _parent : IProperty<Container>; } = label;
		c._parent.set(this);
				
		label.text.set(labelText != null ? labelText : "");
		var linked = label.linkedControl;
		control.ifIs(Control, function(c) linked.set(c));
		
		description = new TextOutput("");
		var c : { private var _parent : IProperty<Container>; } = description;
		c._parent.set(this);
	}
	
	override function _id() : String
	{
		return "F";
	}
	
	override function componentByPath(path : String) : INamedComponent
	{
		var named = control.as(INamedComponent);
		if(null != named && named.path() == path)
			return named;
		var container = control.as(Container);
		if (null != container)
			return container.componentByPath(path);
		return null;
	}
	
	override function componentByName(name : String) : INamedComponent
	{
		var named = control.as(INamedComponent);
		if(null != named && named.name == name)
			return named;
		var container = control.as(Container);
		if (null != container)
			return container.componentByName(name);
		return null;
	}
}

enum LabelPosition {
	Left;
	Top;
}